from app import db
from datetime import datetime

class CourseResource(db.Model):
    __tablename__ = 'course_resource'

    resource_id = db.Column(db.String(36), primary_key=True, comment='资源ID')
    title = db.Column(db.String(100), nullable=False, comment='资源标题')
    resource_type = db.Column(db.String(1), nullable=False, comment='资源类型（1视频 2文档 3音频）')
    url = db.Column(db.String(500), nullable=False, comment='资源URL地址')
    duration = db.Column(db.Integer, comment='时长（秒）')
    size = db.Column(db.BigInteger, comment='文件大小（字节）')
    sort_order = db.Column(db.Integer, default=0, comment='排序号')
    status = db.Column(db.String(1), default='0', comment='状态（0正常 1禁用）')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, comment='更新时间')
    # sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship Product.course_resources - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.
    product_id = db.Column(db.String(36), db.ForeignKey('product.product_id'), nullable=False, comment='关联的商品ID')
    def to_dict(self):
        return {
            'resourceId': self.resource_id,
            'productId': self.product_id,
            'title': self.title,
            'resourceType': self.resource_type,
            'url': self.url,
            'duration': self.duration,
            'size': self.size,
            'sortOrder': self.sort_order,
            'status': self.status,
            'createTime': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
            'updateTime': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else None
        }